#include "playericon.h"

PlayerIcon::PlayerIcon(Player* p)
{
    myPlayer=p;
    setFlag(QGraphicsItem::ItemIsMovable, true);
    this->setX(homex);
    this->setY(homey);
    currentBase=0;
    bounds = new QRectF(-75,-75,150,150);
}

//copy constructor
PlayerIcon::PlayerIcon(PlayerIcon *p){
    myPlayer = p->getPlayer();
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setCurrentBase(p->getCurrentBase());
    bounds = new QRectF(-75,-75,150,150);
}

void PlayerIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                       QWidget *widget)
{
    QColor* backgroundColor= new QColor(70,130,180);//blue
    QColor* fontColor= new QColor(255,255,255);//white
    painter->setPen(backgroundColor->toRgb());
    painter->drawRect(boundingRect());
    painter->fillRect(boundingRect(), backgroundColor->toRgb());
    painter->setPen(fontColor->toRgb());
    painter->drawText(-50,-25,myPlayer->getLastName());
    painter->drawText(-50,20,QString::number(myPlayer->getJerseyNumber()));
}

// Define the bounding rectangle of the object for collision detection
QRectF  PlayerIcon::boundingRect() const
{
    return *bounds;
}

Player* PlayerIcon::getPlayer(){
    return myPlayer;
}

void PlayerIcon::placeOnFirst(){
    this->setX(firstx);
    this->setY(firsty);
    currentBase = 1;
    this->show();
}

void PlayerIcon::placeOnSecond(){
    this->setX(secondx);
    this->setY(secondy);
    currentBase=2;
    this->show();
}

void PlayerIcon::placeOnThird(){
    currentBase = 3;
    this->setX(thirdx);
    this->setY(thirdy);
    this->show();
}

void PlayerIcon::placeOnHome(){
    this->setX(homex);
    this->setY(homey);
    currentBase=0;
    this->show();
}

int PlayerIcon::getCurrentBase(){
    return currentBase;
}

void PlayerIcon::setCurrentBase(int base){
    if(base >= 0 && base < 4){
        currentBase=base;
    }

    switch(base){
    case 0:
        placeOnHome();
        break;
    case 1:
        placeOnFirst();
        break;
    case 2:
        placeOnSecond();
        break;
    case 3:
        placeOnThird();
        break;
    }

}

void PlayerIcon::setBountdingRect(QRectF *rect){
    bounds= rect;
}
